package com.hxjc.bankagent.mvc.controller;

import com.cmbc.afw.security.utils.SecurityUtil;
import com.cmbc.afw.utils.exception.RStatusException;
import com.hxjc.bankagent.security.PostHandler;
import com.hxjc.bankagent.security.SecurityHandler;
import com.hxjc.common.utils.FileLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;


/**
 * TestController
 *
 * @author yinz
 * @date 2018/5/11
 */
@Controller
public class BankFormController  {

    Logger logger = LoggerFactory.getLogger(BankFormController.class);

    @GetMapping("/results")
    public String results() {
        return "results";
    }

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    @GetMapping("/accountaquire")
    public String accountaquire() {

        return "";
    }

    @GetMapping("/testpost")
    public String testpost() {
        String resp = PostHandler.post("http://localhost:8080/postjson");
        System.out.println(resp);
        return "results";
    }

    @PostMapping(value = "/postjson", consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String greetingJson(HttpEntity<String> httpEntity) {
        String json = httpEntity.getBody();
        // json contains the plain json string
        System.out.println("json: " + json);
        return json;
    }

    @PostMapping("/testjson")
    public String testjson (@RequestParam("jsonInput") String name){
        System.out.println("=====================================================");
        System.out.println(name);
        String name2 = "{\"RequestBody\":"+ name + "}";
        //签名
        String sgntr = SecurityHandler.sign(name2);
        System.out.println(sgntr);

        //加密
        String cipherText = SecurityHandler.encrypt(name);

        System.out.println("========================================");
        System.out.println(cipherText);
        return "results";
    }
}
